package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.RedisConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
//表明这个类需要被swagger 扫描 生成接口文档
@Api
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }
    /**
     * employee 员工增加方法 in破落in
     * @return
     */
    @PostMapping
    public Result insertemployee(@RequestBody EmployeeDTO employeeDTO){
        //加入数据层 去进行添加
        return Result.success(employeeService.insertemployee(employeeDTO));
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(StringRedisTemplate stringRedisTemplate) {
        //退出登录 也删除缓存
        stringRedisTemplate.delete(RedisConstant.CACHE_EMPLOYEE);
        return Result.success();
    }

    /**
     * 根据名字进行模糊匹配 模糊查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> selectByPage(EmployeePageQueryDTO employeePageQueryDTO){
        return Result.success(employeeService.selectByPage(employeePageQueryDTO));
    }


    @PostMapping("/status/{status}")
    public Result putByStatus(@PathVariable Integer status,Integer id){
        return Result.success(employeeService.putstatus(status,id));
    }

    /**
     * 修改员工时 进行回显操作
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Employee employees= employeeService.selectByid(id);
        return Result.success(employees);
    }

    /**
     * 进行修改员工操作
     * @param employeeDTO
     * @return
     */
    @PutMapping
    public Result putList(@RequestBody EmployeeDTO employeeDTO){
        return Result.success(employeeService.putList(employeeDTO));
    }















}
